let s:plugin = {}

function! s:plugin.geturl() dict
    if isdirectory(self.directory)
        if has_key(self, 'url')
            return self.url
        else
            let self.job = self.asyncrun('git remote -v', self.directory, self.seturl)
        endif
    endif
    if has_key(self, 'url')
        return self.url
    else
        return ''
    endif
endfunction

function! s:plugin.asyncrun(cmd, directory, callback) dict
    let opts = { 'cwd': a:directory, 'out_cb': a:callback}
    return job_start(a:cmd, opts)
endfunction

function! s:plugin.pull() dict
    let self.job = self.asyncrun('git pull', self.directory, self.output)
endfunction

function! s:plugin.seturl(channel, msg) dict
    let self.url = matchstr(a:msg, 'http\(s\)\?://\S\+')
    call self.output(a:channel, self.url)
endfunction

function! s:plugin.output(channel, msg) dict
    let text = 'git pull ' . a:msg
    let options = #{
				\ line: 1,
				\ col: 100,
				\ minwidth: 100,
				\ time: 3000,
				\ tabpage: -1,
				\ zindex: 300,
				\ drag: 1,
				\ highlight: 'WarningMsg',
				\ border: [],
				\ close: 'click',
				\ padding: [0,1,0,1],
				\ }
    call popup_notification(text, options)
endfunction


let s:self = { 'plugins': [] }

function! s:self.add(directory) dict
    let plugin = deepcopy(s:plugin)
    let plugin.directory = a:directory
    call plugin.geturl()
    call add(self.plugins, plugin)
    return plugin
endfunction

function! s:self.parse(directory, ...) dict
    if a:0 > 0
        let pattern = a:1
    else
        let pattern = '/*/start/*'
    endif
    if isdirectory(a:directory)
        if !has_key(self, 'file')
            let self.file = fnamemodify(a:directory . '/repo.list', ':p') 
        endif
        let async = api#import('async')
        call async.map(self.add, glob(a:directory . pattern, 1, 1))
    endif
endfunction


function! api#repo#get() abort
    return deepcopy(s:self)
endfunction
